//
//  HomeViewController.swift
//  weibo
//
//  Created by hm on 2020/4/18.
//  Copyright © 2020 hm. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {

    // MARK:- 懒加载属性
    private lazy var titleBtn: TitleButton = TitleButton()
    //弹出动画代理
    //weak self作用：解决循环引用问题
    //原因：HomeViewController强引用了popAnimator，popAnimator的闭包又强引用了HomeViewController
    private lazy var popAnimator = PopAnimator { [weak self](presented) -> () in
        //注意：在闭包中使用类的成员变量必须要加self
        //等号左侧一般用可选解包：原因：左侧解包失败后，不会执行赋值操作
        //等号右侧一般用强制解包：原因：等号左侧必须要接受值
        self?.titleBtn.isSelected = presented
    }
    
    // MARK:- 系统回调函数
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //启动转盘动画
        visitorView.addRotationAnim()
        if !isLogin{
            return
        }
        
        //设置顶部导航栏
        setupNavigationBar()
    }

}

// MARK:- 设置UI
extension HomeViewController {
    private func setupNavigationBar() {
        //设置左上角按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention")
        
        //设置右上角按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        
        //设置顶部标题
        titleBtn.setTitle("marqstree", for: .normal)
        titleBtn.addTarget(self, action: #selector(self.titleBtnClick), for: .touchUpInside)
        navigationItem.titleView = titleBtn
    }
}

// MARK:- 事件监听
extension HomeViewController {
    //点击标题时，弹出自定义PopView
    @objc private func titleBtnClick(titlebtn: TitleButton){
        //切换按钮图标
        titleBtn.isSelected = !titlebtn.isSelected
        
        //创建弹出控制器
        let vc = PopOverViewController()
        //设置modal样式
        //不让被覆盖的内容隐藏
        //避免主界面被弹出view删除
        vc.modalPresentationStyle = .custom
        
        //设置转场动画
        //定义弹出PopView的位置及尺寸
        popAnimator.presentedFrame = CGRect(x: 100,y:100,width: 100,height: 200)
        vc.transitioningDelegate = popAnimator
        
        //弹出自定义view
        present(vc,animated: true, completion: nil)
    }
}
